
const matchMenus = (routes, currentRoute) => {
  const selectedKeys = [];
  const targetPathId = [];
  const childrenRouteMatch = (routeList, targetPath) => {
    if (routeList && routeList.length > 0) {
      for (const item of routeList) {
        if (item.path && targetPath.includes(item.path)) {
          return true;
        }
      }
    }
    return false;
  };
  const buildMenus = (routeList, targetPath) => {
    for (const route of routeList) {
      if (
        route.path &&
        targetPath.includes(route.path) &&
        !childrenRouteMatch(route.children, targetPath)
      ) {
        selectedKeys.push(route.key);
        return true;
      }
      if (route.children && route.children.length > 0) {
        if (buildMenus(route.children, targetPath)) {
          targetPathId.push(route.key);
          return true;
        }
      }
    }
    return false;
  };

  buildMenus(routes, currentRoute);

  return {
    currentKeys: selectedKeys,
    openKeys: targetPathId,
  };
};

export default matchMenus;
